#!/bin/sh  

agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"
curl -k -stderr -b tmp$$ -c tmp$$ -A $agent 'https://www.covestor.com/login'  2&>1
export submit="action=1&pass=74d84cd61c230fefdd977cce6cb87f07&d=%2Fmysummary&email=longshort&pa=c690713&Cancel.x=21&Cancel.y=11&Cancel=Login" 
curl -k -stderr -b tmp$$ -c tmp$$ -A $agent -d $submit -e 'https://www.covestor.com/login' 'https://www.covestor.com/login' 2&>1
curl -k -stderr -b tmp$$ -c tmp$$ -A $agent 'http://www.covestor.com/mysummary' 2&>1

>tmpcovest
for stock in `curl -stderr -b tmp$$ -c tmp$$ -A $agent "http://www.covestor.com/stk" |grep "Latest Trade" |egrep -o "\/stk\/[a-z:]+"|grep -v ":" |cut -d'/' -f3`
do
    holder=`curl -k -stderr -b tmp$$ -c tmp$$ -A $agent  "http://www.covestor.com/stk/$stock" |grep  "Latest Trade" |egrep -o "/mbr/[A-Za-z0-9_-]+"`
    certify=`curl -k -stderr -b tmp$$ -c tmp$$ -A $agent  "http://www.covestor.com$holder" |egrep -o "uncertified_data_logo|verified_on"  |sed -e 's/uncertified\_data\_logo/No/g' -e 's/verified\_on/Yes/g'`
    follower=`curl -k -stderr -b tmp$$ -c tmp$$ -A $agent  "http://www.covestor.com/stk/$stock" |grep -A 2 "Latest Trade" |grep followers |egrep -o ">[0-9]+<" |sed -e 's/>//g' -e 's/<//g'`
    uppercase=`echo $stock |tr [a-z] [A-Z]`
    longshort=`curl -stderr -b tmp$$ -c tmp$$ -A $agent  "http://www.covestor.com$holder/$uppercase" |egrep -A 3 "<b>$uppercase</b>" |egrep -i -o "Long|Out|Short"`
    return=`curl -k -stderr -b tmp$$ -c tmp$$ -A $agent "http://www.covestor.com/$holder"  |grep -A 3 "Annualized Return:" |egrep -o "\-[0-9.]+%|[0-9.]+%|n\/a"`
    echo $uppercase $follower $longshort $certify $return $holder |awk '{printf("%s,%s,%s,%s,%3.2f,%s\n",$3,$1,$2,$4,$5,$6)}' >>tmpcovest
done

echo "Action|Stocks|Fans|Verify| Return |    Member     |Patterns"  > CovestList
echo "--------------------------------------------------------------------------------------------"  >> CovestList
cat tmpcovest|sort |sed 's/\/mbr\///g' |while read line
do
   stock=`echo $line |cut -d',' -f2`
   export list=`grep -w $stock *List |cut -d':' -f1|tr '\n' ';'|sed 's/ //g'|cut -c1-80`
   echo $line,$list |awk 'BEGIN { FS = "," } ; {printf("%6s|%6s|%4d|%6s|%8.2f|%16s|%-30s\n"),$1,$2,$3,$4,$5,$6,$7}' >> CovestList
done
echo "--------------------------------------------------------------------------------------------"  >> CovestList
cat CovestList

